package com.jams.goodox.activity.serve;

import android.app.ProgressDialog;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.listen.ActionResultListener;
import com.jams.goodox.utils.EditTextLimitUtils;
import com.jams.goodox.utils.EditTextUtils;
import com.jams.goodox.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 评论、留言
 * <p>
 * create by 顾佳佳 on 2017/05/05
 */
public class CommentActivity extends BaseActivity {

    @Bind(R.id.et_comment)
    EditText etComment;
    @Bind(R.id.tv_send)
    TextView tvSend;
    private String id;
    private String msgId;
    private String toUserId;
    private String toUserName;
    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment_acivity;
    }

    @Override
    protected void init() {
        setTitleVisibility(false);
        id = getIntent().getStringExtra("id");
        msgId = getIntent().getStringExtra("msgId");
        toUserId = getIntent().getStringExtra("toUserId");
        toUserName = getIntent().getStringExtra("toUserName");
        if (toUserName != null) {
            String hint = "回复" + toUserName + "：";
            SpannableStringBuilder sendText = new SpannableStringBuilder(hint);
            sendText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.help_font_color)), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            sendText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.main_theme)), 2, hint.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            sendText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.help_font_color)), hint.length() - 1, hint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            etComment.setHint(sendText);
        }
        tvSend.setClickable(false);
        EditTextLimitUtils.lengthFilter(etComment, 500, "评论不能超过500字");
        etComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().length() > 0) {
                    tvSend.setTextColor(ContextCompat.getColor(CommentActivity.this, R.color.font_main));
                    tvSend.setBackgroundResource(R.drawable.main_theme_radius);
                    tvSend.setClickable(true);
                } else {
                    tvSend.setTextColor(ContextCompat.getColor(CommentActivity.this, R.color.font_white));
                    tvSend.setBackgroundResource(R.drawable.bottom_btn_2dp_grey);
                    tvSend.setClickable(false);
                }
            }
        });
    }

    private void send() {
        if (EditTextUtils.editTextIsEmpty(etComment)) {
            return;
        }
        if (progressDialog == null) {
            progressDialog = ProgressDialog.show(this, "", getString(R.string.is_submitting), true, false);
        } else {
            progressDialog.show();
        }
        tvSend.setClickable(false);
        // 邻里(neighbor),活动(activity),投票(vote)
        PriseCommentUtils.comment(this, id, msgId, etComment.getText().toString().trim(), toUserId, "neighbor", new ActionResultListener() {
            @Override
            public void success() {
                progressDialog.dismiss();
                ToastUtil.show(CommentActivity.this, "评论发送成功");
                setResult(1992);
                finish();
            }

            @Override
            public void failure() {
                progressDialog.dismiss();
                tvSend.setClickable(true);
            }
        });
    }

    @OnClick({R.id.tv_cancel, R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_send:
                send();
                break;
        }
    }
}
